﻿using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using Microsoft.Office.Interop.Excel;

namespace FlyData.CoreCode.Email
{
    public class Email
    {
        public void Send(string senderEmailAddress, string password, string recipiantIndicator, string subject, string messageHeader, string messageFooter, Range excelSelection, bool isMailSentToContactsFromForm)
        {
            var client = GetSmtpClient(senderEmailAddress, password);
            if (isMailSentToContactsFromForm)
            {
                string messageInHtmLformat = new FormatMessage(messageHeader, excelSelection, messageFooter).GetMessageInHtmLFormat();
                sendEmail(senderEmailAddress, recipiantIndicator, subject, client, messageInHtmLformat);
            }
            else
            {
                SendEmailToRecipiantFromExcelRange(senderEmailAddress, recipiantIndicator, subject, messageHeader, messageFooter, excelSelection, client);
            }
        }

        private void SendEmailToRecipiantFromExcelRange(string senderEmailAddress, string recipiantIndicator, string subject,
                                                    string messageHeader, string messageFooter, Range excelSelection,
                                                    SmtpClient client)
        {
            int rowindex = 0;
            Range headerRow = null;
            foreach (Range row in excelSelection.Rows)
            {
                if (rowindex == 0)
                {
                    headerRow = row;
                }
                else
                {
                    string messageInHtmLformat =
                        new FormatMessage(messageHeader, headerRow, row, messageFooter).GetMessageInHtmLFormat();
                    string recipiantAddressFromExcel = GetCellValueFromRange(recipiantIndicator.ToUpper(), row);
                    sendEmail(senderEmailAddress, recipiantAddressFromExcel, subject, client, messageInHtmLformat);
                }
                rowindex = rowindex + 1;
            }
        }


        private static string GetCellValueFromRange(string recipiantIndicator, Range dataRow)
        {
            foreach (Range cell in dataRow.Cells)
            {
                if (cell.Address.Split('$')[1].Equals(recipiantIndicator))
                {
                    return cell.Value2;
                }
            }
            return null;
        }

        private static void sendEmail(string senderEmailAddress, string recipiantIndicator, string subject, SmtpClient client,
                                      string messageInHtmLformat)
        {
            var mailMessage = SetMailMessage(senderEmailAddress, recipiantIndicator, subject, messageInHtmLformat);
            client.Send(mailMessage);
        }


        private static MailMessage SetMailMessage(string senderEmailAddress, string recipiant, string subject, string message)
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(senderEmailAddress);
            mailMessage.To.Add(new MailAddress(recipiant));
            mailMessage.Subject = subject;
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = message;
            return mailMessage;
        }

        private static SmtpClient GetSmtpClient(string senderEmailAddress, string password)
        {
            var client = new SmtpClient("smtp.gmail.com", 587)
                             {
                                 Credentials = new NetworkCredential(senderEmailAddress, password),
                                 EnableSsl = true
                             };
            return client;
        }
    }
}